import { reqGetMajor } from "@/api/reqMajor"
import type { pageForm } from "@/types/base"
import type { Major } from "@/types/major"
import { ref, watch, watchEffect } from "vue"

export const useShowMajor = () => {
  const majorListRef = ref<Major[]>([])
  // 翻页获取数据
  const nowPageRef = ref(1)
  const sizeRef = ref(5)
  const totalRef = ref(10)

  // 用于记录数据变化的Ref
  const changeRef = ref(Symbol())
  // changeRef.value = !changeRef.value

  const getMajorByPage = async (form: pageForm) => {
    const {
      status,
      data: { total, data },
    } = await reqGetMajor(form)
    if (status === 0) {
      majorListRef.value = data
      totalRef.value = total
    }
  }
  //   const getAllMajor = async () => {
  //     const { status, data } = await reqGetAllMajor()
  //     if (status === 0) {
  //       majorListRef.value = data
  //     }
  //   }
  // 什么时候

  //  什么叫做数据响应式
  //   数据的变化，关联的函数会自动执行
  // 依赖收集 / 派发更新
  // watchEffect
  // 默认执行一次
  // 里面相关依赖发生变化会再次执行

  watch(changeRef, () => {
    getMajorByPage({ page: nowPageRef.value, size: sizeRef.value })
  })

  watchEffect(() => {
    // getAllMajor()
    getMajorByPage({ page: nowPageRef.value, size: sizeRef.value })
  })
  return { majorListRef, totalRef, nowPageRef, sizeRef, changeRef }
}
